<?php
class N2t
{
	const ENG 	= 1;
	const SPA 	= 2;
	const POR 	= 3;
	const DEC_SEP	= ".";
	private $_sellang;
	public function N2t() { $this->Initialize(); }	
	private function IsSelLangOK($value)
	{
		switch($value) {
			case self::ENG:
				return true;
			case self::SPA:
				return true;
			case self::POR:
				return true;
		}
		return false;
	}
	private function Initialize()
	{
		$this->_sellang	= self::SPA;
	}
	public function Convert($value, $currency, $front = true, $sel = null)
	{
		$value		= (String)$value;
		$sel		= ($sel == NULL) ? $this->_sellang : $sel;
		$decimal_part	= NULL;
		if (preg_match('/\./', $value)) {
			$_int_dec	= split('\.', $value);
			$decimal_part	= $this->Convert($_int_dec[1], "", $front, $sel);
			$value		= $_int_dec[0];
		}
		
		if (!$this->IsSelLangOK($sel))
			throw new Exception("Not supported language\n");
		$literal	= "";		
		switch($sel) {
			case self::SPA:				
				$literal 	= $this->num2letras((String)$value, false);
				break;
			case self::ENG:
				return "NOT IMPLEMENTED";
			case self::POR:
				return "NOT IMPLEMENTED";
		}
		$literal	= ($decimal_part != NULL) ? "$literal ".$this->DecimalSeparatorLiteral($sel)." $decimal_part" : $literal;
		$literal	= ($front) ? "$currency $literal" : "$literal $currency";

		return $this->Capitalize($literal);
	}
	private function RemoveDots($str)
	{
		//return preg_replace('//')
	}
	private function DecimalSeparatorLiteral($sel)
	{
		switch($sel) {
			case self::SPA:
				return "con";
			case self::ENG:
				return "with";
			case self::POR:
				return "com";
		}
	}
	private function Capitalize($str)
	{
		//le puse un trim por que me traia un espacio al frente fede
		$words	= preg_split("/\s+/", trim($str));		
		$i	= 0;
		foreach($words as &$word) {
			$first		= strtoupper($word[0]);
			$words[$i]	= preg_replace('/^\w/', $first, $word);
			$i++;
		}
		return implode(" ", $words);
	}
	private function num2letras($num, $fem = true, $dec = true) 
	{ 
		//if (strlen($num) > 14) die("El n?mero introducido es demasiado grande"); 
		$matuni[2]  = "dos"; 
		$matuni[3]  = "tres"; 
		$matuni[4]  = "cuatro"; 
		$matuni[5]  = "cinco"; 
		$matuni[6]  = "seis"; 
		$matuni[7]  = "siete"; 
		$matuni[8]  = "ocho"; 
		$matuni[9]  = "nueve"; 
		$matuni[10] = "diez"; 
		$matuni[11] = "once"; 
		$matuni[12] = "doce"; 
		$matuni[13] = "trece"; 
		$matuni[14] = "catorce"; 
		$matuni[15] = "quince"; 
		$matuni[16] = "dieciseis"; 
		$matuni[17] = "diecisiete"; 
		$matuni[18] = "dieciocho"; 
		$matuni[19] = "diecinueve"; 
		$matuni[20] = "veinte"; 
		$matunisub[2] = "dos"; 
		$matunisub[3] = "tres"; 
		$matunisub[4] = "cuatro"; 
		$matunisub[5] = "quin"; 
		$matunisub[6] = "seis"; 
		$matunisub[7] = "sete"; 
		$matunisub[8] = "ocho"; 
		$matunisub[9] = "nove"; 
		
		$matdec[2] = "veint"; 
		$matdec[3] = "treinta"; 
		$matdec[4] = "cuarenta"; 
		$matdec[5] = "cincuenta"; 
		$matdec[6] = "sesenta"; 
		$matdec[7] = "setenta"; 
		$matdec[8] = "ochenta"; 
		$matdec[9] = "noventa"; 
		$matsub[3]  = 'mill'; 
		$matsub[5]  = 'bill'; 
		$matsub[7]  = 'mill'; 
		$matsub[9]  = 'trill'; 
		$matsub[11] = 'mill'; 
		$matsub[13] = 'bill'; 
		$matsub[15] = 'mill'; 
		$matmil[4]  = 'millones'; 
		$matmil[6]  = 'billones'; 
		$matmil[7]  = 'de billones'; 
		$matmil[8]  = 'millones de billones'; 
		$matmil[10] = 'trillones'; 
		$matmil[11] = 'de trillones'; 
		$matmil[12] = 'millones de trillones'; 
		$matmil[13] = 'de trillones'; 
		$matmil[14] = 'billones de trillones'; 
		$matmil[15] = 'de billones de trillones'; 
		$matmil[16] = 'millones de billones de trillones'; 
		
		$num = trim((string)@$num); 
		if ($num[0] == '-') { 
		$neg = 'menos '; 
		$num = substr($num, 1); 
		}else 
		$neg = ''; 
		while ($num[0] == '0') $num = substr($num, 1); 
		if ($num[0] < '1' or $num[0] > 9) $num = '0' . $num; 
		$zeros = true; 
		$punt = false; 
		$ent = ''; 
		$fra = ''; 
		for ($c = 0; $c < strlen($num); $c++) { 
		$n = $num[$c]; 
		if (! (strpos(".,'''", $n) === false)) { 
			if ($punt) break; 
			else{ 
			$punt = true; 
			continue; 
			} 
		
		}elseif (! (strpos('0123456789', $n) === false)) { 
			if ($punt) { 
			if ($n != '0') $zeros = false; 
			$fra .= $n; 
			}else 
		
			$ent .= $n; 
		}else 
		
			break; 
		
		} 
		$ent = '     ' . $ent; 
		if ($dec and $fra and ! $zeros) { 
		$fin = ' coma'; 
		for ($n = 0; $n < strlen($fra); $n++) { 
			if (($s = $fra[$n]) == '0') 
			$fin .= ' cero'; 
			elseif ($s == '1') 
			$fin .= $fem ? ' una' : ' un'; 
			else 
			$fin .= ' ' . $matuni[$s]; 
		} 
		}else 
		$fin = ''; 
		if ((int)$ent === 0) return 'Cero ' . $fin; 
		$tex = ''; 
		$sub = 0; 
		$mils = 0; 
		$neutro = false; 
		while ( ($num = substr($ent, -3)) != '   ') { 
		$ent = substr($ent, 0, -3); 
		if (++$sub < 3 and $fem) { 
			$matuni[1] = 'una'; 
			$subcent = 'as'; 
		}else{ 
			$matuni[1] = $neutro ? 'un' : 'uno'; 
			$subcent = 'os'; 
		} 
		$t = ''; 
		$n2 = substr($num, 1); 
		if ($n2 == '00') { 
		}elseif ($n2 < 21) 
			$t = ' ' . $matuni[(int)$n2]; 
		elseif ($n2 < 30) { 
			$n3 = $num[2]; 
			if ($n3 != 0) $t = 'i' . $matuni[$n3]; 
			$n2 = $num[1]; 
			$t = ' ' . $matdec[$n2] . $t; 
		}else{ 
			$n3 = $num[2]; 
			if ($n3 != 0) $t = ' y ' . $matuni[$n3]; 
			$n2 = $num[1]; 
			$t = ' ' . $matdec[$n2] . $t; 
		} 
		$n = $num[0]; 
		if ($n == 1) { 
			$t = ' ciento' . $t; 
		}elseif ($n == 5){ 
			$t = ' ' . $matunisub[$n] . 'ient' . $subcent . $t; 
		}elseif ($n != 0){ 
			$t = ' ' . $matunisub[$n] . 'cient' . $subcent . $t; 
		} 
		if ($sub == 1) { 
		}elseif (! isset($matsub[$sub])) { 
			if ($num == 1) { 
			$t = ' mil'; 
			}elseif ($num > 1){ 
			$t .= ' mil'; 
			} 
		}elseif ($num == 1) { 
			$t .= ' ' . $matsub[$sub] . "on"; 
		}elseif ($num > 1){ 
			$t .= ' ' . $matsub[$sub] . 'ones'; 
		}   
		if ($num == '000') $mils ++; 
		elseif ($mils != 0) { 
			if (isset($matmil[$sub])) $t .= ' ' . $matmil[$sub]; 
			$mils = 0; 
		} 
		$neutro = true; 
		$tex = $t . $tex; 
		} 
		$tex = $neg . substr($tex, 1) . $fin; 
		return ucfirst($tex); 
	}
}
// $a = new N2t();
// echo $a->Convert(13.72 , "Gs.");
?>